package com.lab303.powerproject.util;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

public class WriteExcel {

    private static HashMap<String,String> testChs  = new HashMap<String,String>();


    public static void doWrite(Map<String,float[]> data, String filePath, String sheetName) throws IOException {
        testChs.put("temperature","计算温度");
        testChs.put("humidity","计算湿度");
        testChs.put("dianliang","计算电量");
        testChs.put("xuqiu","计算需求量");
        testChs.put("xinfeng","计算新风");
        testChs.put("xuneng","计算水蓄能");
        testChs.put("dimaiguan","计算地埋管");
        testChs.put("zhujizhigong","计算主机直供");
        testChs.put("zhujixuleng","计算主机蓄冷");
        testChs.put("nuandian","计算暖电");
        testChs.put("guangre","计算光热");

        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet(sheetName);
        int rowNum = 0;
        XSSFRow row = sheet.createRow(rowNum);
        XSSFCell cell;
        for (int i = 0 ; i< data.size();i++){
            cell = row.createCell(i);
            cell.setCellValue(testChs.get(data.keySet().toArray()[i]));
        }
        XSSFRow[] rows = new XSSFRow[24];
        for (int i = 1 ; i <= 24;i++){
            rows[i-1] = sheet.createRow(i);
            int cellNum = 0;
            for (float[] dataArray:data.values()){
                cell = rows[i-1].createCell(cellNum);
                cellNum++;
                cell.setCellValue(dataArray[i-1]);
            }
        }

        OutputStream outputStream = new FileOutputStream(new File(filePath));
        workbook.write(outputStream);
        outputStream.close();
        workbook.close();
    }
}
